Ext.onReady(function(){
	Ext.QuickTips.init();
	
	var dataArray = eval("("+photoData+")");
	
	/**
	 * 需要加载的数据
	 */
	var loadData = new Array();
	
	/**
	 * 单条记录
	 */
	var singleRecord;
	
	/**
	 * 组装数据源
	 */
	for (var i=0; i<dataArray.length; i++) {
		singleRecord = new Array();
		
		if(dataArray[i]!=null) {
			singleRecord[0] = parseInt(dataArray[i].CL_CODE);
	      	singleRecord[1] = dataArray[i].PL_CODE;
	      	singleRecord[2] = dataArray[i].CL_NAME;
	      	singleRecord[3] = dataArray[i].CL_PCODE;
	      	singleRecord[4] = dataArray[i].PARM;
	      	singleRecord[5] = parseInt(dataArray[i].IDX);
	      	singleRecord[6] = dataArray[i].REMARKED;
	      	
	      	loadData[i] = singleRecord;
		}
	}
		
    var store = new Ext.data.ArrayStore({
        proxy   : new Ext.data.MemoryProxy(),
        fields  : ['CL_CODE', 'PL_CODE', 'CL_NAME', 'CL_PCODE', 'PARM', 'IDX', 'REMARKED'],
        sortInfo: {
            field    : 'IDX',
            direction: 'ASC'
        }
    });
            
    store.loadData(loadData);
    
    /**
     * 加载菜单图标
     */
    var dataview = new Ext.DataView({
        store: store,
        style:'background: url(images/welcome.png) repeat;padding-left:30px',
        tpl  : new Ext.XTemplate(
            '<ul>',
                '<tpl for=".">',
                    '<li class="phone">',
                        '<img width="90" height="90" src="{PARM}" />',
                        '<strong>{CL_NAME}</strong>',
                    '</li>',
                '</tpl>',
            '</ul>'
        ),
        
        plugins : [
            new Ext.ux.DataViewTransition({
                duration  : 550,
                idProperty: 'CL_CODE'
            })
        ],
        
        id: 'phones',
        itemSelector: 'li.phone',
        overClass   : 'phone-hover',
        singleSelect: true,
        multiSelect : true,
        autoScroll  : true,
        
        listeners:{'click':function() {
        		var records = this.getSelectedRecords();
        		
        		var clCode = records[0].data.CL_CODE;
        		var linkUrl = records[0].data.REMARKED;
        		var clName = records[0].data.CL_NAME;
        		
		    	var tab=null;
		    	var id = 'tab-' + clCode;
		    	tab=Ext.getCmp(id);
		    		    	
		    	if(!tab){
		    		tab=rightTab.add(new Ext.Panel({
			                id: id,
			                title: clName,
			                layout: 'fit',
			                html:'<iframe id="if_dbsy"'+clCode+' src="'+linkUrl
			                	+'" height="100%" width="100%" frameborder="0" top="0" left="0"></iframe>',
			                closable: true
			            }));
		            rightTab.setActiveTab(tab);
		    	}else{
		    		rightTab.setActiveTab(tab);
		    	}
		    	mainPanel.doLayout();
        	}
        }
    });
       
    /**
     * 欢迎使用Panel
     */
    welcomePanel = new Ext.Panel({
        layout: 'fit',
        items : dataview,
        width : 1000,
        renderTo: 'welcomebody'
    });
        
//    doSort();
    
    /**
     * 排序
     */
    function doSort() {
        store.sort(getSorters(), "ASC");
        setTimeout(bindToStore,500);
    };
    
    /**
     * 隔500毫秒绑定到dataview以实现动态效果
     */
    function bindToStore() {
    	dataview.bindStore(store);
    }
    
    /**
     * 获取排序器
     */
    function getSorters() {
        var sorters = [{
            field: 'IDX',
            direction: 'ASC'
        }];
    
        return sorters;
    }

	/**
	 * 定义页面对应的后台处理类完整路径。
	 */
	var clsUrl=basePath + 'JarpListener?className=cn.com.zsc.system.Index';
	
	/**
	 * 数据源
	 */
	var projectReader=new Ext.data.JsonReader({
		fields:[
		    {name :'DEPT_CODE'},
    		{name :'DEPT_NAME'}
    	]
	});
	
	/**
	 * 从后台读取数据到数据源
	 */
	var projectStore=new Ext.data.Store({
		url:clsUrl+'&methodName=getProjectData&paramCount=1&param_0='+userID,
		reader:projectReader
	});
	
	
	/**
	 * 标题panel
	 */
	var topPanel =  new Ext.Panel({ 
            id: 'topPanel',
            height:90,
            region:'north',
            border:false,
            bodyStyle:'background-image:url(images/top.jpg);',
            layout:'hbox',
            layoutConfig: {
            	padding:'10',
                pack:'end',
            	align:'middle'
            },
            defaults:{margins:'0 10 -20 0'},
            items:[
            	{name:'btn1',tooltip:'修改密码',id:'btn1',xtype:'button',boder:false,iconCls: 'tlpwblue',scale: 'large',
	            	handler:function() {
	            		formPanel.getForm().reset();
		      	   		win.setTitle("修改密码");
		      	   		win.show();
		      	   	},
		      	   	listeners:{
	            		'mouseout':function() {
	            			Ext.getCmp("btn1").setIconClass('tlpwblue');
	            		},
	            		'mouseover':function() {
	            			Ext.getCmp("btn1").setIconClass('tlpwred');
	            		}
	            	}
	            },
            	{name:'btn2',tooltip:'关于我们',id:'btn2',xtype:'button',boder:false,iconCls:'tlinfoblue',scale: 'large',
            		handler:function() {
	            		info.show();
		      	   	},
            		listeners:{
	            		'mouseout':function() {
	            			Ext.getCmp("btn2").setIconClass('tlinfoblue');
	            		},
	            		'mouseover':function() {
	            			Ext.getCmp("btn2").setIconClass('tlinfored');
	            		}
	            	}
	            },
            	{name:'btn3',tooltip:'退出系统',id:'btn3',xtype:'button',boder:false,iconCls: 'tlexitblue',scale: 'large',
	            	handler:function() {
	            		Ext.Msg.show({
							title:'提示',
							msg:'退出系统?', 
							buttons: Ext.Msg.YESNO,
				            icon: Ext.MessageBox.QUESTION,
							fn:function(btn){
								if( "yes" == btn){
									window.location.href='logout.jsp';
								}
							}
						});
	            	},
	            	listeners:{
	            		'mouseout':function() {
	            			Ext.getCmp("btn3").setIconClass('tlexitblue');
	            		},
	            		'mouseover':function() {
	            			Ext.getCmp("btn3").setIconClass('tlexitred');
	            		}
	            	}
            	}],
            html:'<table border="0" height="100%" align="right" width="50%">' +
            		'<tr height="70%">' +
            			'<td></td>' +
            			'<td></td>' +
            		'</tr>' +
            		'<tr>' +
            			'<td></td>' +
            			'<td align="right" width="90%">当前用户：'+name+'&nbsp;&nbsp;&nbsp;&nbsp;当前日期：<span id="localtime"></span></td>' +
            		'</tr>' +
            	 '</table>'
    });
    
    /**
	 * formPanel
	 */
	var formPanel = new Ext.FormPanel({
		bodyStyle:'padding:20px 15px 0',
		region:'center',
		layout:'form',
		frame:true,
		labelWidth:100,
		labelAlign:'right',
		items:[
		       {fieldLabel: '旧密码',width:200,name:'oldPassword',id:'oldPassword',xtype:'textfield',inputType :'password'},
		       {fieldLabel: '新密码',width:200,name:'newPassword',id:'newPassword',xtype:'textfield',inputType :'password'},
		       {fieldLabel: '重复新密码',width:200,name:'repeatpwd',id:'repeatpwd',xtype:'textfield',inputType :'password'}
		]
	});
	
	/**
	 * infoPanel
	 */
	var infoPanel = new Ext.FormPanel({
		bodyStyle:'padding:20px 15px 0',
		region:'center',
		layout:'form',
		frame:true,
		labelWidth:50,
		labelAlign:'right',
		items:[
		       {width:200,xtype:'label',html:'版本号:1.0.0'}
		]
	});
	
	/**
	 * 修改密码对话框
	 */
	var win = new Ext.Window({
		width:400,
		height:220,
		layout:'border',
		closeAction:'hide',
		modal:true,
		draggable:false,
		resizable:false,
		tbar:[
		      {
		    	  text:'保存',
		    	  iconCls:'save',
		    	  handler:changePassword
		      },
		      '-',
		      {
		    	  text:'取消',
		    	  iconCls:'cancel',
		    	  handler:function(){
      	   			win.hide();
		      	  }
		      }
		],
		items:[formPanel]
	});
	
	/**
	 * 关于我们对话框
	 */
	var info = new Ext.Window({
		title:'关于我们',
		width:300,
		height:120,
		layout:'border',
		closeAction:'hide',
		modal:true,
		draggable:false,
		resizable:false,
		items:[infoPanel]
	});
	
	function changePassword() {
		if(Ext.getCmp("oldPassword").getValue()==""){
			Ext.getCmp("oldPassword").markInvalid("请输入旧密码");
			return false;
		}
		
		if(Ext.getCmp("newPassword").getValue()==""){
			Ext.getCmp("newPassword").markInvalid("请输入新密码");
			return false;
		}
		
		if(Ext.getCmp("repeatpwd").getValue()==""){
			Ext.getCmp("repeatpwd").markInvalid("请重复输入新密码");
			return false;
		}
		
		if(Ext.getCmp("newPassword").getValue() != Ext.getCmp("repeatpwd").getValue()){
			Ext.getCmp("repeatpwd").markInvalid("密码不一致，请重新输入");
			return false;
		}
		
		var obj = new Object();
		
		obj.userID = userID;
		obj.oldPassword = Ext.getCmp("oldPassword").getValue();
		obj.newPassword = Ext.getCmp("newPassword").getValue();
		
		var url=clsUrl+'&methodName=changePassword&paramCount=0'
		
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onSaveRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function onSaveRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示', 
		    	   		width : 200,
		   				msg : '修改成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
					
		   			formPanel.getForm().reset();
		   			win.hide();
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',
		    	   		width : 200,
		   				msg : httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
 
    /**
     * 下方右侧TAB页
     */
    var rightTab= new Ext.TabPanel({
	  		id:'rightTab',
	        region:'center',
	        split:true,
	        resizeTabs:true,
	        border:false,
	        minTabWidth: 115,
	        tabWidth:135,
	        deferredRender : false,
	        enableTabScroll:true,
	        defaults: {autoScroll:true},
	        disabled:false
  	});
	
  	/**
  	 * 右侧Panel
  	 */
    var rightPanel=new Ext.Panel({
            id: 'rightPanel',
            region:'center',
            width:230,
            activeOnTop:false,
            //collapsible:true,
            layout:'border',
//            items:[topBar,rightTab]
            items:[rightTab]
    });
    
    /**
     * 左侧Panel
     */
	var leftPanel=new Ext.Panel({
            id: 'leftPanel',
            region:'west',
            title:'功能菜单',
            width:230,
            split:true,
            fill:false,
            border:false,
            activeOnTop:false,
            collapsible:true,
            layout:'accordion',
            layoutConfig:{
                animate:true
            }
    }); 
   
	/**
     * 下方panel
     */
    var mainPanel=new Ext.Panel({
            id: 'mainPanel',
            region:'center',
            layout: 'border',  
            items:[leftPanel,rightPanel]
    }); 
	
    /**
     * 页面布局类
     */
    var viewport = new Ext.Viewport({   
	        layout:'border',   
	        items:[   
	        	topPanel,
	          	mainPanel
	        ]   
	});
	
	/**
	 * 功能菜单点击处理函数
	 */
	function menuClick(node){
    	if(!node.attributes.leaf){
			return;
		}
		
    	var tab=null;
    	var id = 'tab-' + node.attributes.id;
    	tab=Ext.getCmp(id);
    	
    	if(!tab){
    		tab=rightTab.add(new Ext.Panel({
	                id: id,
	                title: node.attributes.text,
	                layout: 'fit',
	                html:'<iframe id="if_dbsy"'+node.attributes.id+' src="'+node.attributes.linkUrl
	                	+'" height="100%" width="100%" frameborder="0" top="0" left="0"></iframe>',
	                closable: true
	            }));
            rightTab.setActiveTab(tab);
    	}else{
    		rightTab.setActiveTab(tab);
    	}
    	mainPanel.doLayout();
    }
    
	/**
	 * 根据用户所属角色的权限动态加载功能菜单
	 */
	function initGroupPanel(store,records){
    	for(var i=0;i<records.length;i++){
    		var id=records[i].get("DEPT_CODE");
    		var name=records[i].get("DEPT_NAME");
    		
	    	var group=new Ext.tree.TreePanel({
		    	id:'grouptree'+id,
		    	title:name,
		    	autoScroll:true,
		        border:true,
		        frame:false,
		        width:240,
		        animate:true,
		        collapsible: true,
		        height:'100%',
		        rootVisible:false,
		        loader: new Ext.tree.TreeLoader({
		        	url:clsUrl+'&methodName=getMenuTree&paramCount=2&param_0='+id+'&param_1='+userID
		        }),
		       	root:new Ext.tree.AsyncTreeNode({
						        text: '系统菜单',
						        id:'grouptree+'+id+'-1'
			   			 })
		    	
		    });
		    group.on("expand",function(p){
		    	p.root.reload();
		    });
		    group.on("click",function(node){
		    	menuClick(node);
		    });
	    	leftPanel.add(group);
	    	group=null;
    	}
    	leftPanel.doLayout();
    	if(leftPanel.items.first()){
    		leftPanel.items.first().expand();
    	}
    	
    	var tab = rightTab.add(welcomePanel);
       	rightTab.setActiveTab(tab);
		rightTab.items.items[0].setTitle("欢迎使用");
    }
    
    /**
     * 数据加载前显示
     */
    projectStore.on("beforeload",function(){
		 Ext.getCmp('leftPanel').body.mask('功能菜单加载中', 'x-mask-loading');
	});
	
	/**
	 * 数据加载中显示
	 */
	projectStore.on("load",function(store,records){
		initGroupPanel(store,records);
		Ext.getCmp('leftPanel').body.unmask();
	});
	
	projectStore.load();
	
	function showLocale(objD) {
		var str, colorhead, colorfoot;
		
		var yy = objD.getYear();
		if (yy < 1900)
			yy = yy + 1900;
			
		var MM = objD.getMonth() + 1;
		if (MM < 10)
			MM = '0' + MM;
			
		var dd = objD.getDate();
		if (dd < 10)
			dd = '0' + dd;
			
		var hh = objD.getHours();
		if (hh < 10)
			hh = '0' + hh;
			
		var mm = objD.getMinutes();
		if (mm < 10)
			mm = '0' + mm;
			
		var ss = objD.getSeconds();
		if (ss < 10)
			ss = '0' + ss;
			
		var ww = objD.getDay();
		if (ww == 0)
			colorhead = "<font color=\"#FF0000\">";
		if (ww > 0 && ww < 6)
			colorhead = "<font color=\"#373737\">";
		if (ww == 6)
			colorhead = "<font color=\"#008000\">";
		if (ww == 0)
			ww = "星期日";
		if (ww == 1)
			ww = "星期一";
		if (ww == 2)
			ww = "星期二";
		if (ww == 3)
			ww = "星期三";
		if (ww == 4)
			ww = "星期四";
		if (ww == 5)
			ww = "星期五";
		if (ww == 6)
			ww = "星期六";
			
		colorfoot = "</font>"
		
		str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":"
				+ ss + "  " + ww + colorfoot;
				
		return (str);
	}
	
	function tick() {
		var today;
		today = new Date();
		document.getElementById("localtime").innerHTML = showLocale(today);
		window.setTimeout(tick, 1000);
	}
	
	tick();
});